<?php
/*
 * @Author: 偻儸小卒[EdisonLiu_]
 * @Date: 2019-08-02 09:24:10
 * @LastEditors: 偻儸小卒[EdisonLiu_]
 * @LastEditTime: 2019-08-03 11:37:06
 * @Description: 
 */

namespace App\Http\Controllers\Admin;

// use Spatie\Permission\Models\Role;
// use Spatie\Permission\Models\Permission;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\StoreRolesRequest;
use App\Http\Requests\Admin\UpdateRolesRequest;
use App\Http\Models\Admin\Permission_;
use App\Http\Models\Admin\Role_;
use App\Http\Models\Admin\ClassifyPermissions;

class RolesController extends Controller
{
    /**
     * Display a listing of Role.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        if (! Gate::allows('watch roles')) {
            return abort(401);
        }

        $roles = Role_::all();
        $Permissions=new Permission_();

        $Permissions = $Permissions->with(['hasDetails'=>function($query){
            // $query->with(['hasManyDetails'=>function($qu_det){
            //     $qu_det->with('hasDetails');
            // }]);
        }])->get();
        
        return view('admin.manage.roles.index', compact('roles','Permissions'));
    }

    /**
     * Show the form for creating new Role.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        if (! Gate::allows('create roles')) {
            return abort(401);
        }
        $permissions = Permission_::get()->pluck('name', 'name');
        // $Permissions=new Permissions();

        // $Permissions = $Permissions->with(['classifyHasMany'=>function($query){
        //     $query->with(['hasManyDetails'=>function($qu_det){
        //         $qu_det->with('hasDetails');
        //     }]);
        // }])->get();
        
        $classify_per=new ClassifyPermissions();

        $classify = $classify_per->where(array('parent_id' => 0))->orderBy('rank', 'DESC')->with(['classifyHasMany'=>function($query){
            $query->with(['hasManyDetails'=>function($qu_det){
                $qu_det->with('hasDetails');
            }]);
        }])->get();


        // dd($permissions);die;
        return view('admin.manage.roles.create', compact('role','classify'));
    }

    /**
     * Store a newly created Role in storage.
     *
     * @param  \App\Http\Requests\StoreRolesRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreRolesRequest $request)
    {
        if (! Gate::allows('create roles')) {
            return abort(401);
        }
        $role = Role_::create($request->except('permission'));
        $permissions = $request->input('permission') ? $request->input('permission') : [];
        $role->givePermissionTo($permissions);

        return redirect()->route('admin.manage.roles.index');
    }


    /**
     * Show the form for editing Role.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        if (! Gate::allows('update roles')) {
            return abort(401);
        }
        // $permissions = Permission_::get()->pluck('name', 'name');


        $classify_per=new ClassifyPermissions();

        $classify = $classify_per->where(array('parent_id' => 0))->orderBy('rank', 'DESC')->with(['classifyHasMany'=>function($query){
            $query->with(['hasManyDetails'=>function($qu_det){
                $qu_det->with('hasDetails');
            }]);
        }])->get();

        $role = Role_::findOrFail($id);

        $permissions= $role->permissions()->pluck('name', 'name');

        return view('admin.manage.roles.edit', compact('role', 'permissions','classify'));
    }

    /**
     * Update Role in storage.
     *
     * @param  \App\Http\Requests\UpdateRolesRequest  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateRolesRequest $request, $id)
    {
        if (! Gate::allows('update roles')) {
            return abort(401);
        }
        $role = Role_::findOrFail($id);
        $role->update($request->except('permission'));
        $permissions = $request->input('permission') ? $request->input('permission') : [];
        $role->syncPermissions($permissions);

        return redirect()->route('admin.manage.roles.index');
    }


    /**
     * Remove Role from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        if (! Gate::allows('delete roles')) {
            return abort(401);
        }
        $role = Role_::findOrFail($id);
        $role->delete();

        return redirect()->route('admin.manage.roles.index');
    }

    /**
     * Delete all selected Role at once.
     *
     * @param Request $request
     */
    public function massDestroy(Request $request)
    {
        if (! Gate::allows('delete roles')) {
            return abort(401);
        }
        if ($request->input('ids')) {
            $entries = Role_::whereIn('id', $request->input('ids'))->get();

            foreach ($entries as $entry) {
                $entry->delete();
            }
        }
    }

}
